package com.mjf.web.controller.common;

// import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mjf.cjt.service.VoucherService;
import com.mjf.common.utils.ResponseUtils;
import com.mjf.common.utils.ResponseVO;
import com.mjf.system.domain.BalanceRecord;
import com.mjf.system.service.IBalanceRecordService;

import com.mjf.system.service.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
    import java.util.List;
import java.util.Map;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2025/4/13 18:00
 */
@RestController
@RequestMapping("/balance/record")
public class BalanceRecordController {

    @Autowired
    private IBalanceRecordService balanceRecordService;
    @Autowired
    private VoucherService voucherService;
    /**
     * 查询余额记录
     * accountingperiod 会计期间
     * @return
     */
    @GetMapping("")
    public ResponseVO<IPage<BalanceRecord>> getBalanceRecordList(@RequestParam(value = "page") Integer page,
                                                                 @RequestParam(value = "size") Integer size,
                                                                 @RequestParam(value = "accountingperiod", required = false) String accountingperiod) {

        return ResponseUtils.success(balanceRecordService.getBalanceRecordList(page, size, accountingperiod));
    }

    /**
     * 项目收入明细账计算
     * @return
     */
    @PostMapping("/jurmx")
    public ResponseVO<Void> jurMx(@RequestBody XmsrmxReqVO vo) {
        vo.setPage(1);
        vo.setSize(10000000);
        balanceRecordService.jurMx(vo);
        return ResponseUtils.success();
    }

    /**
     * 项目收入明细账
     * @return
     */
    @PostMapping("/xmsrmx")
    public ResponseVO<Map<String, Object>> getXmsrmxList(@RequestBody XmsrmxReqVO vo) {
        return ResponseUtils.success(balanceRecordService.getXmsrmxList(vo));
    }

    /**
     * 项目收入汇总导出
     * @return
     */
    @PostMapping("/xmsrhz/exp")
    public void getXmsrhzExp(@RequestBody XmsrmxReqVO vo, HttpServletResponse response) throws IOException {
        vo.setPage(1);
        vo.setSize(10000000);
        Map<String, Object> res = balanceRecordService.getXmsrhzList(vo);
        List<XmsrhzVO> vos = (List<XmsrhzVO>) res.get("list");
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        // 写入到输出流
        EasyExcel.write(response.getOutputStream(), XmsrhzVO.class)
            .sheet("项目收入汇总")
            .doWrite(vos);
        outputStream.close();
    }

    /**
     * 项目收入汇总
     * @return
     */
    @PostMapping("/xmsrhz")
    public ResponseVO<Map<String, Object>> getXmsrhzList(@RequestBody XmsrmxReqVO vo) {
        return ResponseUtils.success(balanceRecordService.getXmsrhzList(vo));
    }

    /**
     * 项目收入明细账导出
     * @return
     */
    @PostMapping("/xmsrmx/exp")
    public void getXmsrmxExp(@RequestBody XmsrmxReqVO vo, HttpServletResponse response) throws IOException {
        vo.setPage(1);
        vo.setSize(100000);
        Map<String, Object> res = balanceRecordService.getXmsrmxList(vo);
        List<XmsrmxVO> vos = (List<XmsrmxVO>) res.get("list");
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        // 写入到输出流
        EasyExcel.write(response.getOutputStream(), XmsrmxVO.class)
            .sheet("项目收入明细账")
            .doWrite(vos);
        outputStream.close();
    }

    /**
     * 查询成本利润分析
     * accountingperiod 会计期间
     * @return
     */
    @PostMapping("/cblrfx/exp")
    public void getCblrfxList(@RequestBody CblrfxReqVO vo, HttpServletResponse response) throws IOException {
        List<CblrfxVO> vos = balanceRecordService.getCblrfxList(vo.getAccountingperiods());
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        // 写入到输出流
        EasyExcel.write(response.getOutputStream(), CblrfxVO.class)
            .sheet("成本利润分析")
            .doWrite(vos);
        outputStream.close();
    }

    /**
     * 查询成本利润分析
     * accountingperiod 会计期间
     * @return
     */
    @PostMapping("/cblrfx")
    public ResponseVO<List<CblrfxVO>> getCblrfxList(@RequestBody CblrfxReqVO vo) {
        return ResponseUtils.success(balanceRecordService.getCblrfxList(vo.getAccountingperiods()));
    }


    /**
     * 计算期初余额
     */
    @PutMapping("/jur")
    public ResponseVO<String> jur(@RequestBody BalanceJurVO balanceJurVO) {
        balanceRecordService.jur(balanceJurVO);
        return ResponseUtils.success("计算成功");
    }
}
